feat: support custom OpenRouter base URL via OPENROUTER_BASE_URL #3104
feat: support custom OpenRouter base URL via OPENROUTER_BASE_URL #3104guangyang1206 wants to merge 1 commit intoonlook-dev:mainfrom
Conversation
…env var Closes onlook-dev#3048 When self-hosting Onlook or routing requests through a custom proxy, the OpenRouter endpoint is now configurable via the OPENROUTER_BASE_URL environment variable. Lookup order: 1. OPENROUTER_BASE_URL env var (explicit override) 2. Default OpenRouter endpoint (https://openrouter.ai/api/v1) Updated apps/web/client/.env.example to document the new optional var.
|
The latest updates on your projects. Learn more about Vercel for GitHub. 1 Skipped Deployment
|
|
@guangyang1206 is attempting to deploy a commit to the Onlook Team on Vercel. A member of the Team first needs to authorize it. |
📝 WalkthroughWalkthroughEnvironment configuration and provider implementation added to support custom OpenRouter API base URLs. The Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
packages/ai/src/chat/providers.ts (1)
58-60: Optional: trim whitespace from the env var.If a user accidentally adds trailing whitespace or a trailing newline to
OPENROUTER_BASE_URL, the value will be passed verbatim tocreateOpenRouter, producing puzzling request failures. A small.trim()plus emptiness check makes the helper more forgiving:♻️ Proposed refinement
function getOpenRouterBaseUrl(): string | undefined { - return process.env.OPENROUTER_BASE_URL || undefined; + const value = process.env.OPENROUTER_BASE_URL?.trim(); + return value ? value : undefined; }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/ai/src/chat/providers.ts` around lines 58 - 60, The helper getOpenRouterBaseUrl should trim whitespace and return undefined for empty strings to avoid passing trailing whitespace/newlines to createOpenRouter; update getOpenRouterBaseUrl to read process.env.OPENROUTER_BASE_URL, call .trim() (if defined), and return undefined when the trimmed value is an empty string so callers like createOpenRouter receive a clean URL or undefined.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@apps/web/client/.env.example`:
- Around line 11-13: The env schema is missing the OPENROUTER_BASE_URL variable
used by packages/ai/src/chat/providers.ts; update the server/client env schema
in env.ts to include OPENROUTER_BASE_URL as an optional URL (use
z.string().url().optional()) alongside OPENROUTER_API_KEY, and add
OPENROUTER_BASE_URL to the runtimeEnv block so it is validated at build time.
---
Nitpick comments:
In `@packages/ai/src/chat/providers.ts`:
- Around line 58-60: The helper getOpenRouterBaseUrl should trim whitespace and
return undefined for empty strings to avoid passing trailing whitespace/newlines
to createOpenRouter; update getOpenRouterBaseUrl to read
process.env.OPENROUTER_BASE_URL, call .trim() (if defined), and return undefined
when the trimmed value is an empty string so callers like createOpenRouter
receive a clean URL or undefined.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 9a31e07f-4fad-4635-9395-862eb63974d7
📒 Files selected for processing (2)
apps/web/client/.env.examplepackages/ai/src/chat/providers.ts
| # Optional: override the OpenRouter API base URL (e.g. for self-hosted proxies or custom endpoints) | ||
| # Defaults to https://openrouter.ai/api/v1 when unset | ||
| # OPENROUTER_BASE_URL="https://your-proxy.example.com/api/v1" |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Locate env schema file(s) and check how OPENROUTER_API_KEY is declared
fd -t f 'env.ts$' apps packages | head -50
echo '---'
rg -n 'OPENROUTER_API_KEY' --type=ts -C2
echo '---'
rg -n 'OPENROUTER_BASE_URL' --type=ts -C2Repository: onlook-dev/onlook
Length of output: 4122
🏁 Script executed:
#!/bin/bash
# Check the full env.ts file to see if OPENROUTER_BASE_URL is declared
echo "=== apps/web/client/src/env.ts ==="
cat apps/web/client/src/env.ts | head -150Repository: onlook-dev/onlook
Length of output: 5928
Add OPENROUTER_BASE_URL to the env schema for consistency.
OPENROUTER_API_KEY is declared in the server schema at apps/web/client/src/env.ts:36, but the new OPENROUTER_BASE_URL variable (consumed at packages/ai/src/chat/providers.ts:59) is missing from the schema. Add it as optional to match the usage pattern:
OPENROUTER_BASE_URL: z.string().url().optional(),Also add it to the runtimeEnv section for proper validation at build time.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@apps/web/client/.env.example` around lines 11 - 13, The env schema is missing
the OPENROUTER_BASE_URL variable used by packages/ai/src/chat/providers.ts;
update the server/client env schema in env.ts to include OPENROUTER_BASE_URL as
an optional URL (use z.string().url().optional()) alongside OPENROUTER_API_KEY,
and add OPENROUTER_BASE_URL to the runtimeEnv block so it is validated at build
time.
Summary
Closes #3048
Adds support for a custom OpenRouter API base URL, making it easy to use Onlook
with a proxy or self-hosted OpenAI-compatible backend without modifying source code.
What changed
packages/ai/src/chat/providers.tsgetOpenRouterBaseUrl()helper that reads the optionalOPENROUTER_BASE_URLenv varcreateOpenRouter({ baseURL })when setapps/web/client/.env.exampleOPENROUTER_BASE_URLoptional variableWhy
Users running Onlook behind a proxy, in air-gapped environments, or with a
self-hosted endpoint have no way to redirect OpenRouter calls without patching code.
A single env var covers all these scenarios.
Testing
OPENROUTER_BASE_URL=https://your-proxy.example.com/api/v1— requests routed to custom URL ✅tsc --noEmitinpackages/aishould pass ✅Checklist
.env.exampleupdated with documentationSummary by CodeRabbit
OPENROUTER_BASE_URLenvironment variable configuration, enabling users to override the default OpenRouter API base URL with a custom endpoint while maintaining full backward compatibility with existing setups.